package com.nulldev.util.web.nCaptcha.captchas.obftext;

import java.awt.image.BufferedImage;

import com.nulldev.util.image.ImageUtil;
import com.nulldev.util.image2.textobf.TextObf;
import com.nulldev.util.image2.textobf.TextObf.ObfuscationMethod;
import com.nulldev.util.java.DataSet;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.math.MathUtil;
import com.nulldev.util.other.BooleanUtil;
import com.nulldev.util.string.StringsUtil;
import com.nulldev.util.web.nCaptcha.backend.Captcha;

public class TrueFalseStatement implements Captcha {

	@Override
	public DataSet<String, String[]> generateBase64Image(final int botLevel) {
		final int i0 = MathUtil.randInt(-200, 200);
		final int i1 = MathUtil.randInt(-200, 200);
		final boolean x = BooleanUtil.random();
		final boolean expected = (x ? i0 > i1 : i1 < i0);
		final BufferedImage obf = TextObf.obfuscate((x ? i0  + " > " + i1 : i1 + " < " + i0)
				, (botLevel > 1 ? ObfuscationMethod.Noise : ObfuscationMethod.PixelObfuscation), 
				FontManager.getFontSet(MathUtil.randInt(14, 16)));
		final String base64 = ImageUtil.encodeToString(obf, "png");
		return new DataSet<String, String[]>("data:image/png;base64," + base64, 
				(expected ? new String[] {"true", "yes", "TRUE", "YES"} : new String[] {"false", "FALSE", "no", "NO"}));
	}

	@Override
	public String getType() {
		return "truefalse";
	}
}
